﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace QL_NOI_TRU
{
    public partial class frmNhapKetQuaXetNghiem : DevExpress.XtraEditors.XtraForm
    {
        public frmNhapKetQuaXetNghiem()
        {
            InitializeComponent();
        }

        private void frmNhapKetQuaXetNghiem_Load(object sender, EventArgs e)
        {
            //đổ dữ liệu vào gridview
            var kn = new Ketnoi(frmDangnhap.stringConn);
            gridControl1.DataSource = kn.ExcuteQuery("select MAPHIEU as 'Mã phiếu điều trị',TENDV as 'Tên dịch vụ',GIOXN as 'Giờ xét nghiệm',NGAYXN as 'Ngày xét nghiệm',KQXN as 'Kết quả xét nghiệm' from KQ_CANLAMSANG,DV_CAN_LAM_SANG where DV_CAN_LAM_SANG.MADV = KQ_CANLAMSANG.MADV");

            // đổ dữ liệu vào cbbmaDv
            var maDV = "select * from DV_CAN_LAM_SANG";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = maDV;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                var newVariable = cbbMaDV.Properties.Items.Add(String.Format("{0} | {1}", dr["MADV"], dr["TENDV"]));
            }
            dr.Close();
        }

        private void cbbMaDV_SelectedIndexChanged(object sender, EventArgs e)
        {
            var maPhieu = "select MAPHIEU from KQ_CANLAMSANG where MADV = '"+cbbMaDV.Text.Substring(0,10).Trim()+"'";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = maPhieu;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                var newVariable = cbbMaPhieu.Properties.Items.Add(String.Format("{0}", dr["MAPHIEU"]));
            }
        }
        /// <summary>
        /// lưu dữ liệu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string strr = "EXEC Insert_KQ_CANLAMSANG @MAPHIEU='" + cbbMaPhieu.Text.ToString().Trim() + "',@MADV='" + cbbMaDV.Text.Substring(0, 10).Trim() + "',@GIOXN='" + txtGioXN.Text.ToString() + "',@NGAYXN='" + cbbNgayXN.Text.ToString() + "',@KQXN=N'" + txtKetQua.Text.ToString() + "'";
            var kn = new Ketnoi(frmDangnhap.stringConn);
            kn.ExcuteQuery(strr);
         //   MessageBox.Show("Đã lưu thành công!");
            btRefreshGia_Click(null, null);
        }

        private void btRefreshGia_Click(object sender, EventArgs e)
        {
            cbbMaDV.SelectedIndex = -1;
            cbbMaPhieu.SelectedIndex = -1;
            cbbNgayXN.Text = "";
            txtKetQua.Text = "";
            txtGioXN.Text = "";
            frmNhapKetQuaXetNghiem_Load(null, null);
        }



        /// <summary>
        /// bắt sự kiện keypress enter
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void cbbMaDV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                cbbMaPhieu.Focus();
        }

        private void cbbMaPhieu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                txtGioXN.Focus();
        }

        private void txtGioXN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                cbbNgayXN.Focus();
        }

        private void cbbNgayXN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                txtKetQua.Focus();
        }

        private void txtKetQua_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                btLuu.Focus();
        }

        
    }
}